﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.IO;
using Whir.Common.WebHelper;

public partial class Whir_System_Module_Template_Main : System.Web.UI.Page
{
    private int itemLevel = -1;
    string[] lable = new string[3] { "┣", "┗", "┃" };

    //记录当前级别的个数，用于排序的
    Dictionary<int, bool> dictIsEnd = new Dictionary<int, bool>();

    protected void Page_Load(object sender, EventArgs e)
    {
        RoleHelper.IsLogin();    //登录判断

        if (!IsPostBack)
        {
            BindData();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        BindColumn();
        BindTemplate();
    }

    
    /// <summary>
    /// 绑定栏目
    /// </summary>
    private void BindColumn()
    {
        string strSQL = String.Format("SELECT columnID, columnName, parentID, defaultTemp, listTemp, contentTemp, sort FROM Whir_Column");
        DataTable dt = Whir.SQLServerDAL.DbHelperSQL.Query(strSQL).Tables[0];

        if (dt.Rows.Count > 0)
        {
            AddClilcItem(dt, "0", "0");
        }
    }

    private void AddClilcItem(DataTable dt, string parentID, string pparentID)
    {
        itemLevel += 1;

        DataView dv = dt.DefaultView;
        dv.RowFilter = "parentID=" + parentID;
        dv.Sort = "sort ASC";

        for (int i = 0; i < dv.Count; i++)
        {
            if (dictIsEnd.ContainsKey(itemLevel))
                dictIsEnd[itemLevel] = i == dv.Count - 1;
            else
                dictIsEnd[itemLevel] = i == dv.Count - 1;

            ListItem item = new ListItem();
            item.Value = dv[i]["columnID"].ToString();
            item.Text = String.Format("{0} {1} ({2}) ({3}) ({4})",
                i == dv.Count - 1 ? GetLable() + lable[1] : GetLable() + lable[0],
                dv[i]["columnName"],
                dv[i]["defaultTemp"].ToString() == string.Empty ? "暂无模板" : dv[i]["defaultTemp"],
                dv[i]["listTemp"].ToString() == string.Empty ? "暂无模板" : dv[i]["listTemp"],
                dv[i]["contentTemp"].ToString() == string.Empty ? "暂无模板" : dv[i]["contentTemp"]);
            lbColumn.Items.Add(item);
            AddClilcItem(dt, dv[i]["columnID"].ToString(), dv[i]["parentID"].ToString());
        }

        itemLevel -= 1;
        dv.RowFilter = "parentID=" + pparentID;
    }

    protected string GetLable()
    {
        string strTemp = "";
        for (int i = 0; i < itemLevel; i++)
        {
            if (dictIsEnd[i])
                strTemp += "　";
            else
                strTemp += lable[2];
        }

        return strTemp;
    }


    //绑定模板
    private void BindTemplate()
    {
        string dir = Server.MapPath("~/UploadFiles/template/cn");

        if (Directory.Exists(dir))
        {
            string[] Dirs = Directory.GetFileSystemEntries(dir);
            foreach (string d in Dirs)
            {
                if (File.Exists(d))
                {
                    //文件
                    string[] names = d.Split('\\');
                    FileInfo fileInfo = new FileInfo(d);

                    string filepath = d.Substring(Server.MapPath("~/UploadFiles/template/").Length);

                    ListItem item = new ListItem();
                    item.Text = names[names.Length - 1];
                    item.Value = names[names.Length - 1];

                    lbdefaultTemp.Items.Add(item);
                    lblistTemp.Items.Add(item);
                    lbcontentTemp.Items.Add(item);
                }
            }
        }
    }


    /// <summary>
    /// 匹配模板按钮
    /// </summary>
    protected void btnBind_Click(object sender, EventArgs e)
    {
        if (lbColumn.SelectedIndex == -1)
        {
            JsHelper.Alert(Page, "请选择栏目！");
            return;
        }

        string columnID = lbColumn.SelectedValue;
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column column = bll.GetModel(int.Parse(columnID));

        Button btn = sender as Button;
        string cmd = btn.CommandName;

        switch (cmd)
        {
            case "default":
                column.defaultTemp = lbdefaultTemp.SelectedItem.Text;
                break;
            case "list":
                column.listTemp = lblistTemp.SelectedItem.Text;
                break;
            case "content":
                column.contentTemp = lbcontentTemp.SelectedItem.Text;
                break;
            default:
                break;
        }

        bll.Update(column);
        JsHelper.redirect(Page, "匹配成功！", "Main.aspx");
    }

    /// <summary>
    /// 取消按钮
    /// </summary>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (lbColumn.SelectedIndex == -1)
        {
            JsHelper.Alert(Page, "请选择栏目！");
            return;
        }

        string columnID = lbColumn.SelectedValue;
        Whir.BLL.Whir_Column bll = new Whir.BLL.Whir_Column();
        Whir.Model.Whir_Column column = bll.GetModel(int.Parse(columnID));

        Button btn = sender as Button;
        string cmd = btn.CommandName;

        switch (cmd)
        {
            case "default":
                column.defaultTemp = "";
                break;
            case "list":
                column.listTemp = "";
                break;
            case "content":
                column.contentTemp = "";
                break;
            case "all":
                column.defaultTemp = "";
                column.listTemp = "";
                column.contentTemp = "";
                break;
            default:
                break;
        }

        bll.Update(column);
        JsHelper.redirect(Page, "取消成功！", "Main.aspx");
    }
}
